package leetCode;

import java.util.concurrent.CountDownLatch;

/**
 * 斐波那契数列 两种方法
 * 1：递归（过慢）
 * 2：循环（推荐）理解为把第二个数赋给第一个，把要求的数赋给第二个，永远看成是第一个数加第二个数
 */
public class Fibonacci {

    public static int FibonacciFunc(int n) {
        if (n == 0) {
            return 0;
        }
        if (n == 1) {
            return 1;
        }
        return FibonacciFunc(n - 1) + FibonacciFunc(n - 2);
    }

    public static int FibonacciFunc2(int n) {
        int[] res = {0, 1};
        if (n < 2) {
            return res[n];
        }
        int first = 0;
        int sec = 1;
        int fib = 0;
        for (int i = 2; i <= n; i++) {
            fib = first + sec;
            first = sec;
            sec = fib;
        }
        return fib;
    }

    public static void main(String[] args) throws InterruptedException {
//        int i = FibonacciFunc2(5);
//        System.out.println(i);

        CountDownLatch countDownLatch = new CountDownLatch(5);
        int i = 5;
        for (int j = 0;j<i;j++){
            Thread t = new Thread(new Runnable() {
                @Override
                public void run() {
                    System.out.println("-");
                    countDownLatch.countDown();
                }
            });
            t.start();
        }
        countDownLatch.await();

    }

}
